import {
  fetchHome
} from '../../services/home/home';
import {
  fetchGoodsList
} from '../../services/good/fetchGoods';

Page({
  data: {
    swiperImages: [
      'https://gitee.com/super_yangfeng/yimg/raw/master/wps1.png',
      'https://gitee.com/super_yangfeng/yimg/raw/master/wps2.png',
      'https://gitee.com/super_yangfeng/yimg/raw/master/wps3.png',
    ],
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'scaleToFill'
    },
    goodListPagination: {
      index: 0,
      num: 20,
    },
    privateData: {
      tabIndex: 0,
    }
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({
      swiper,
      tabList
    }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.setData({
      'privateData.tabIndex': e.detail,
    });
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.data.goodListPagination.num;
    let pageIndex = this.data.privateData.tabIndex * pageSize + this.data.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const nextList = await fetchGoodsList(pageIndex, pageSize);
      this.setData({
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
        'goodListPagination.index': pageIndex,
        'goodListPagination.num': pageSize,
      });
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  navigateToIntroduction(event) {
    const index = event.currentTarget.dataset.index;
    wx.navigateTo({
      url: `/pages/introduction${index + 1}/introduction${index + 1}`,
    });
  },

  goodListClickHandle(e) {
    const {
      index
    } = e.detail;
    const {
      spuId
    } = this.data.goodsList[index];
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}`,
    });
  },

  navToActivityDetail({
    detail
  }) {
    const {
      index: promotionID = 0
    } = detail || {};
    wx.navigateTo({
      url: `/pages/introduction/introduction${promotionID}`,
    });
  },
});